package com.hxzy.shop.marketing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.shop.common.dto.marketing.req.DistributionLevelSaveReq;
import com.hxzy.shop.constant.AuthConstant;

import com.hxzy.shop.entity.DistributionLevel;
import com.hxzy.shop.marketing.mapper.DistributionLevelMapper;

import com.hxzy.shop.marketing.service.DistributionLevelService;
import com.hxzy.shop.message.DistributionDrugChangeMessage;
import com.hxzy.shop.utils.WebUtil;
import com.hxzy.shop.vo.ApiException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ Author     ：dididi
 * @ Date       ：Created in 16:30 2023/8/2
 * @ Modified By：
 */
@Service
public class DistributionLevelServiceImpl extends ServiceImpl<DistributionLevelMapper, DistributionLevel> implements DistributionLevelService {


    @Resource                       //distributionLevelMapper
    private DistributionLevelMapper distributionLevelMapper;
    @Override
    public List<DistributionLevel> levelList() {
        List<DistributionLevel> distributionLevels = distributionLevelMapper.selectList(new LambdaQueryWrapper<DistributionLevel>().eq(DistributionLevel::getDisable, false));
        if (ObjectUtil.isEmpty(distributionLevels)) {
            return distributionLevels;
        }
        for (DistributionLevel level : distributionLevels) {
            System.out.println(level);
        }
        distributionLevels.stream().map((level) -> {
                    if (ObjectUtil.isNotEmpty(level.getDiscountRate())  && level.getDiscountRateStatus()) {
                        System.out.println(level.getDiscountRate());
                        BigDecimal bigDecimal = BigDecimal.valueOf(level.getDiscountRate()).setScale(2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(10));
                        level.setDiscountRate(bigDecimal.doubleValue());
                    }
                    return level;
                })
                .sorted(Comparator.comparing(DistributionLevel::getDistributionLevel)).collect(Collectors.toList());

        return distributionLevels;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Object levelSave(List<DistributionLevelSaveReq> reqs) {

        //Long merchantId = Long.valueOf(WebUtil.getRequest().getHeader(AuthConstant.MERCHANT_ID));
        List<DistributionLevel> distributionLevels = this.distributionLevelMapper.selectList(new LambdaQueryWrapper<DistributionLevel>().eq(DistributionLevel::getDisable, false));
        //1,将分销等级表全部删除
        for (DistributionLevel distributionLevel : distributionLevels) {
            distributionLevel.setDisable(true);
        }
        //分销等级不能重复
        Set<Integer> levelSet = new HashSet<>();
        for (DistributionLevelSaveReq req : reqs) {
            levelSet.add(req.getDistributionLevel());
        }
        if (reqs.size()!= levelList().size()){
            throw new ApiException(500,"分销等级不能重复");
        }
        //批量更新
        super.updateBatchById(distributionLevels);
        //重新添加
        List<DistributionLevel> levelList = new ArrayList<>();
        for (DistributionLevelSaveReq req : reqs) {
        DistributionLevel level = new DistributionLevel();
        level.setDistributionLevel(req.getDistributionLevel());
        level.setLevelName(req.getLevelName());
        //营业额的状态
        if (req.getTurnoverStatus()){
            level.setTurnoverStatus(true);
            level.setTurnover(req.getTurnover());
        }else {
            level.setTurnoverStatus(false);
            level.setTurnover(BigDecimal.valueOf(0));
        }
        //邀请人数状态
        if (req.getInviteNumStatus()){
            level.setInviteNumStatus(true);
            level.setInviteNum(req.getInviteNum());
        }else {
            level.setInviteNumStatus(false);
            level.setInviteNum(0);
        }
        //佣金比例状态
        if (req.getCommissionRateStatus()){
            level.setCommissionRateStatus(true);
            BigDecimal bigDecimal = BigDecimal.valueOf(req.getCommissionRate() / 100);
            if (req.getCommissionRate().doubleValue() >= 100.0){
                throw new ApiException(500,"佣金比例不能超过100");
            }
            level.setCommissionRate(bigDecimal);
        }else {
            level.setDiscountRateStatus(false);
            level.setDiscountRate(0.0);
        }
        levelList.add(level);
        }
        //批量保存
        super.saveBatch(levelList);
        //重新编辑 分销药关联返利规则

        return true;
    }
}
